package com.giscloud.dynamicsql.service.impl;

import com.giscloud.dynamicsql.dao.DynamicSqlCfgMapper;
import com.giscloud.dynamicsql.dao.DynamicSqlMapper;
import com.giscloud.dynamicsql.entity.DynamicSqlCfg;
import com.giscloud.dynamicsql.entity.DynamicSqlCfgExample;
import com.giscloud.dynamicsql.service.DynamicSqlService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("dynamicSqlService")
public class DynamicSqlServiceImpl implements DynamicSqlService {

    @Autowired
    private DynamicSqlMapper dynamicSqlMapper;
    @Autowired
    private DynamicSqlCfgMapper dynamicSqlCfgMapper;


    @Override
    public DynamicSqlCfg queryDynamicSqlCfg(String urlType) {
        DynamicSqlCfgExample dynamicSqlCfgExample = new DynamicSqlCfgExample();
        dynamicSqlCfgExample.createCriteria().andUrlTypeEqualTo(urlType);
        List<DynamicSqlCfg> list = dynamicSqlCfgMapper.selectByExample(dynamicSqlCfgExample);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<DynamicSqlCfg> queryDynamicSqlCfgList(DynamicSqlCfg dynamicSqlCfg) {
        DynamicSqlCfgExample dynamicSqlCfgExample = new DynamicSqlCfgExample();
        DynamicSqlCfgExample.Criteria criteria = dynamicSqlCfgExample.createCriteria();
        if(StringUtils.isNotBlank(dynamicSqlCfg.getUrlType())){
            criteria.andUrlTypeEqualTo(dynamicSqlCfg.getUrlType());
        }
        if(StringUtils.isNotBlank(dynamicSqlCfg.getModule())){
            criteria.andModuleEqualTo(dynamicSqlCfg.getModule());
        }
        dynamicSqlCfgExample.setOrderByClause("id");
        List<DynamicSqlCfg> list = dynamicSqlCfgMapper.selectByExample(dynamicSqlCfgExample);
        return list;
    }


    @Override
    public List<Map<String, Object>> dynamicQuery(Map<String, Object> params) {
        return dynamicSqlMapper.dynamicQuery(params);
    }
}
